package com.qf.controller;

import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.utils.ImgCodeFactory;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class LoginController {
    @Resource
    UserService userService;


    @RequestMapping("/getImgCode")
    public Map imgCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 反会json的MIME类型
        resp.setContentType("application/json;charset=utf-8");
        // 告诉浏览器，我这个谁都可以拿
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Methods", "*");
        // 禁止图像缓存。
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", -1);
        resp.setContentType("image/jpeg");

        //获取验证码map
        Map<String, Object> map = ImgCodeFactory.generateCodeAndPic();
        //验证码保存在session
        String codeText = map.get("code").toString();
        req.getSession().setAttribute("codeText", codeText);
        OutputStream out = resp.getOutputStream();
        //输出验证码图片
        ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
        out.close();
        return null;
    }

    @PostMapping("/login")
    public Map login(String username, String password, String imgCode, HttpSession session) {

        String realImgCode = (String) session.getAttribute("codeText");
        //返回到前端的数据
        Map<String, Object> respMsg = new HashMap<>();
        if (!imgCode.equals(realImgCode)) {
            respMsg.put("code", "1");
            respMsg.put("msg", "验证码错误");
            return respMsg;
        }
        User user = userService.getUserByPwd(username, password);

        if (user == null) {
            respMsg.put("code", "3");
            respMsg.put("msg", "登陆失败,用户密码错误或未激活");
            return respMsg;
        }
        respMsg.put("code", "2");
        respMsg.put("msg", "登陆成功");
        respMsg.put("userInfo", user);
        session.setAttribute("user", user);
        return respMsg;

    }


    @GetMapping("/login")
    public Map checkLogin(String method, String settle, HttpSession session,
                          HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String orderStr = settle;


        if (method.equals("login")) {//登录验证
            Map<String, Object> respMsg = new HashMap<>();
            Object obj = session.getAttribute("user");
            if (obj == null) {
                respMsg.put("code", "1");
                respMsg.put("msg", "用户没有登陆");
                if (null != orderStr) {
                    resp.sendRedirect("login.jsp");
                }
                return respMsg;
            } else {
                User user = (User) obj;
                respMsg.put("code", "2");
                respMsg.put("username", user.getUName());
                return respMsg;
            }
        } else if (method.equals("quit")) {//退出登录
            //清除session
            session.invalidate();
        }
            return null;

    }



}

